<template>
    <div class="album">
        <!-- 专辑列表 -->
        <div class="album_list">
            <ul>
                <li v-for="(item, index) in albums" :key="index">
                    <div class="info" @click="$router.push(`/albumlist/${item.id}`)">
                        <img :src="item.picUrl" alt="" style="width:130px;">
                        <i :title="item.name" class="mask"></i>
                        <div class="show">
                            <i class="play"></i>
                        </div>
                    </div>
                    <p :title="item.name" class="dec ellipsis" @click="$router.push(`/albumlist/${item.id}`)" v-html="highlight(item.name)"></p>
                    <span :title="item.artist.name" class="name" @click="$router.push(`/artlist/${item.artist.id}`)">{{ item.artist.name }}</span>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
import { AlbumSearchApi } from "@/request/api";
export default {
    data () {
        return {
            albums: [],
        }
    },
    computed: {
        keyword() {
            return this.$route.params.keyword;
        }
    },
    watch: {
        keyword: {
            handler(newValue, oldValue){
                if(newValue !== oldValue){
                    AlbumSearchApi(newValue).then((res) => {
                        // console.log(res);
                        this.albums = res.data.result.albums;
                    });
                }
            },
            immediate: true,
        }
    },
    // created() {
    //     AlbumSearchApi(this.keyword).then((res) => {
    //         // console.log(res);
    //         this.albums = res.data.result.albums;
    //     });
    // },
    methods: {
        highlight(str) {
            // 正则表达式
            //中间的字符只是字符串
            // 如果需要动态创建一个正则表达式
            // new RegExp(str, rules) str是正则字符串，rules是匹配模式写在字符串中
            const reg = new RegExp(this.keyword, 'gi')
            const highStr = str.replace(reg, `<span style="color: #0c73c2;">${this.keyword}</span>`)
            return highStr
        },
    }
}
</script>
 
<style lang = "less" scoped>
@import "../../assets/base.less"; 
.album {
    .album_list {
        ul {
            flex-wrap: wrap;
            display: flex;
            justify-content: flex-start !important;
        }
        li {
            cursor: pointer;
            width: 160px;
            height: 180px;
            margin: 0 30px 30px 0;
            line-height: 1.5;
            &:nth-of-type(5n) {
                margin-right: 0;
            }
            .info {
                position: relative;
                height: 130px;
                width: 160px;
                overflow: hidden;
                .mask {
                    position: absolute;
                    top: 0;
                    left: 0;
                    height: 130px;
                    width: 160px;
                    display: block;
                    background: url("../../assets/images/public/coverall.png") no-repeat 0px -845px;
                }
                &:hover {
                    .show {
                        display: block;
                    }
                    img {
                        transform: scale(1.1);
                    }
                }
                .show {
                    display: none;
                    i {
                        position: absolute;
                        bottom: 5px;
                        right: 40px;
                        height: 30px;
                        width: 30px;
                        display: block;;
                        float: right;
                        background: url("../../assets/images/public/iconall.png") no-repeat;
                    }
                    .play {
                        background-position: 0px -140px;
                        &:hover {
                            background-position: 0px -170px;
                        }
                    }
                }
            }
            .dec {
                width: 100%;
                font-size: 14px;
                color: #000;
                &:hover {
                    text-decoration: underline;
                }
            }
            .name {
                font-size: 12px;
                color: #666;
                &:hover {
                    text-decoration: underline;
                    color: #666;
                }
            }
        }
    }
}
</style>